<template>
    <van-popup v-model:show="showPopup" position="bottom" round @closed="onPopupClosed">
        <div class="share-pop">
            <div class="fe-title">
                <h3>分享到</h3>
            </div>
            <div class="list">
                <div v-for="(item, i) in shareList" :key="i" class="item" @click="item.fn(shareData)">
                    <div class="outer">
                        <div>
                            <img v-if="item.icon" :src="item.icon" />
                        </div>
                        <p>{{ item.name }}</p>
                    </div>
                </div>
            </div>
            <div class="fe-bottom">
                <div class="confirm" @click="onPopupClosed">
                    取消
                </div>
            </div>
        </div>
        <div class="van-safe-area-bottom"></div>
    </van-popup>
</template>

<script lang="ts" setup>
import { ref, watch } from 'vue';
import { Popup as VanPopup, Dialog } from 'vant';
import { share, downloadFile } from '@/utils/native';
import { shareDataModel, shareListModel } from '@/views/products/brandBreakout/breakout.module';
import { PropType } from 'vue';
import { toRefs } from 'vue';
import { getImageUrl, isWx, copyText } from '@/utils/index';

const props = defineProps({
    isShow: {
        type: Boolean,
        require: true,
        default: false,
    },
    shareData: {
        type: Object as PropType<shareDataModel>,
        require: true,
        default: () => ({})
    },
    isShowDownload: {
        type: Boolean,
        default: true,
    },
});

const { shareData } = toRefs(props);

const emit = defineEmits<{(e: 'close'): void; (e: 'download'): void; }>();

const showPopup = ref(false);

const onPopupClosed = () => {
    emit('close');
};

const miniHandleShare = (href: string) => {
    copyText(href, () => {
        Dialog.alert({
            title: '资料链接已复制到粘贴板',
            message: href,
        }).then(() => undefined).catch(() => undefined);
    });
};

const shareList = ref<shareListModel[]>(
    [
        {
            icon: getImageUrl('qiwei.png'),
            name: '企业微信',
            fn: async (shareData: shareDataModel): Promise<void> => {
                const type: number = [null, undefined, ''].includes(String(shareData.type)) ? 1 : shareData.type;

                const wxStr: string = await isWx() as string;
                if (wxStr === 'mini-wx') {
                    miniHandleShare(shareData.href || '');
                } else {

                    await share({
                        ...shareData,
                        provider: 'qywx',
                        type: type,
                        title: shareData.title,
                        href: shareData.href,
                        imgUrl: shareData.href,
                    });
                }
                onPopupClosed();
            }
        },
        {
            icon: getImageUrl('weixin.png'),
            name: '微信好友',
            fn: async (shareData: shareDataModel): Promise<void> => {
                const type: number = [null, undefined, ''].includes(String(shareData.type)) ? 1 : shareData.type;
                const wxStr = await isWx();
                if (wxStr === 'mini-wx') {
                    miniHandleShare(shareData.href || '');
                } else {

                    await share({
                        ...shareData,
                        provider: 'wx',
                        type: type,
                        title: shareData.title,
                        href: shareData.href,
                        imgUrl: shareData.href,
                    });
                }
                onPopupClosed();
            }
        },
        {
            icon: getImageUrl('download.png'),
            name: '下载',
            fn: async (shareData: shareDataModel): Promise<void> => {
                const wxStr = await isWx();
                const url = (shareData.downloadUrl?shareData.downloadUrl:shareData.href) || '';
                if (wxStr === 'mini-wx') {
                    miniHandleShare(url );
                } else {
                    await downloadFile({ url: url , params: { showProgress: true } });
                }
                onPopupClosed();
            }
        }
    ]
);

!props.isShowDownload && shareList.value.splice(2, 1);

watch(
    () => props.isShow,
    (newVal) => {
        showPopup.value = newVal;
    }
);
</script>

<style scoped lang="scss">
.share-pop {
    background: #f8f8f8;

    .fe-title {
        display: flex;
        padding-right: 12px;
        margin-top: 20px;
        background: #ffffff;

        h3 {
            height: 22px;
            // font-family: PingFangSC-SNaNpxibold;
            font-weight: 600;
            font-size: 16px;
            color: #333333;
            text-align: center;
            flex: 1;
        }
    }

    .list {
        padding: 16px 0 20px;
        display: flex;
        justify-content: space-between;
        background: #ffffff;

        .item {
            flex: 1;
            display: flex;
            justify-content: center;

            .outer {
                text-align: center;

                div {
                    width: 64px;
                    height: 64px;
                    background: #f3f6fa;
                    border-radius: 50%;
                    display: flex;
                    justify-content: center;
                    align-items: center;

                    img {
                        width: 38px;
                        height: 38px;
                    }
                }

                p {
                    height: 24px;
                    display: inline-block;
                    font-weight: 500;
                    font-size: 13px;
                    color: #5b5b5b;
                    line-height: 24px;
                    margin-top: 8px;
                }
            }
        }
    }

    .fe-bottom {
        height: 48px;
        margin-top: 10px;
        text-align: center;
        line-height: 48px;
        font-weight: 400;
        font-size: 14px;
        color: #b5b5b5;
        background: #ffffff;
        padding-bottom: 39px;
    }
}
</style>

